<?
class ItemBO extends BusinessObject{
    
  var $ITEM_TYPE_TOUR     = 0;  //tours or attractions 
  var $ITEM_TYPE_GO       = 1;  //GoAirlink
  var $ITEM_TYPE_TICKET   = 2;  //old broadway tickets
  var $ITEM_TYPE_MEAL     = 3;  //meal. Planet Hollywood
  var $ITEM_TYPE_BROADWAY = 4;  //new broadway tickets  
    
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"                =>	"item_id",
			"title"			    =>	"item_title",
			"comments"          =>	"item_comments",
			"code"              =>	"item_code",
			"adult_price"       =>	"item_adult_price",
			"child_price"       =>	"item_child_price",
			"html"              =>	"item_html",
			"provider"          =>  "item_provider",
			"voucher"           =>  "item_voucher",
			"need_date"         =>  "item_need_date", 
			"need_time"         =>  "item_need_time", 
			"type"			    =>	"item_type",
			"have_blackouts"    =>  "item_have_blackouts", 
			"is_certain_days"   =>  "item_is_certain_days", 
			"monday"            =>  "item_monday", 
			"tuesday"           =>  "item_tuesday", 
			"wednesday"         =>  "item_wednesday",
			"thursday"          =>  "item_thursday", 
			"friday"            =>  "item_friday", 
			"saturday"          =>  "item_saturday", 
			"sunday"            =>  "item_sunday", 
			"disabled"          =>	"item_disabled",
			"deleted"           =>	"item_deleted",
			"child_age_range"   =>  "item_child_age_range",
			"email"             =>	"item_email",
			"url"               =>  "item_url",  
      		"adult_com_price"   =>  "item_adult_com_price",
      		"child_com_price"   =>  "item_child_com_price",
      		"cutoff_exist"      =>  "item_cutoff_exist",
      		"cutoff_value"      => 	"item_cutoff_value",
      		"addprice_exist"    =>  "item_addprice_exist",
      		"price_monday"      =>  "item_price_monday",
      		"price_tuesday"     =>  "item_price_tuesday",
      		"price_wednesday"   =>  "item_price_wednesday",
      		"price_thursday"    =>  "item_price_thursday",
      		"price_friday"      =>  "item_price_friday",
      		"price_saturday"    =>  "item_price_saturday",
      		"price_sunday"      =>  "item_price_sunday",
      		"addprice_adult"    =>  "item_addprice_adult",
      		"addprice_child"    =>  "item_addprice_child",
      		"addpricecom_adult" =>  "item_addpricecom_adult",
      		"addpricecom_child" =>  "item_addpricecom_child",
      		"seodescr"			=>	"item_seodescr",
            "seokeywords"		=>	"item_seokeywords",
            "only_for"			=>  "item_only_for",
    		"is_new"			=>  "item_isnew",
            "per_day"           =>  "item_perday",
            "per_week"          =>  "item_perweek",
            "per_month"         =>  "item_month",
            "per_year"          =>  "item_year",
            //"stops_map"           =>  "item_tourmap",
            "is_package"        =>  "item_is_package",
            "is_tour"  =>  "item_tourattraction",
            "tour_length"  =>  "item_tour_length",
            "departure_location"  =>  "item_departure_location",
            "departure_time"  =>  "item_departure_time",
            "location"  =>  "item_location",
            "hours_of_operation"  =>  "item_hours_of_operation",
            "average_visit_time"  =>  "item_average_visit_time",
            "description"  =>  "item_description",
            "note"  =>  "item_note",
            "note_level"  =>  "item_note_level",
            "note_combo"  =>  "item_note_combo",
            "note_level_combo"  =>  "item_note_level_combo",
            "image1"  =>  "item_image1",
            "image1_w"  =>  "item_image1_w",
            "image1_h"  =>  "item_image1_h",
            "image2"  =>  "item_image2",
            "image2_w"  =>  "item_image2_w",
            "image2_h"  =>  "item_image2_h",
            "image3"  =>  "item_image3",
            "image3_w"  =>  "item_image3_w",
            "image3_h"  =>  "item_image3_h",
            "is_featured"  =>  "item_is_featured",
            "featured_descr"  =>  "item_featured_descr",
            "featured_img"  =>  "item_featured_img",
            "featured_position"  =>  "item_featured_position",
            "small_image"  =>  "item_small_image",
            "tab1_title"  =>  "item_tab1_title",
            "tab1_article"  =>  "item_tab1_article",
            "tab2_title"  =>  "item_tab2_title",
            "tab2_article"  =>  "item_tab2_article",
            "tab3_title"  =>  "item_tab3_title",
            "tab3_article"  =>  "item_tab3_article",
            "tab4_title"  =>  "item_tab4_title",
            "tab4_article"  =>  "item_tab4_article",
            "tab5_title"  =>  "item_tab5_title",
            "tab5_article"  =>  "item_tab5_article",
            "adult_cash_deposit"  =>  "item_adult_cash_deposit",
            "adult_balance_due"  =>  "item_adult_balance_due",
            "kids_cash_deposit"  =>  "item_kids_cash_deposit",
            "kids_balance_due"  =>  "item_kids_balance_due"
		);

		$it = new SiteDiscountItemBO ();
		$dsk = new RelatedBO ();
		$cmb = new ComboBO();
		$pi = new ProvItemBO();
		$this->relations = array(
				"{$it->tableName}" => "{$this->tableName}.{$this->fields["id"]} = {$it->tableName}.{$it->fields["item"]}",
				"{$dsk->tableName}" => "{$this->tableName}.{$this->fields["id"]} = {$dsk->tableName}.{$dsk->fields["related"]}",
		        "{$cmb->tableName}" => "{$this->tableName}.{$this->fields["id"]} = {$cmb->tableName}.{$cmb->fields["related"]}",
				"{$pi->tableName}" => "{$this->tableName}.{$this->fields["id"]} = {$pi->tableName}.{$pi->fields["item"]}"
		);
		
    BusinessObject::setAttributes();
  }
  /* constuctor */
  function ItemBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("items");
    $this->setAttributes();
  }

  /*  Save  */
  function Save($itemID){
    if(is_numeric($itemID) && $itemID<>0)
    {
      $this->id = "$itemID";
      $this->Update("{$this->fields[id]} = '$itemID'");
    } else{
      $this->Insert();
      $this->id = $this->getInsertID();
    }
  }
  
  function checkCode(){    
    $item = new ItemBO();
    $item->code = $this->code;
    $item->site = $this->site;
	  $item->deleted = 0;
    $item->Load(0,1);
    if($item->Next()){
      if($item->id != $this->id) return(false);
    }
    return(true);
  }
  
  /*
    Return price which depends from date/affiliate/agent/discounts
  	$userID  - current user ID. 0 - visitor.
  	$date    - estimated date
	$adults  - count of adults 
    $childs  - count of childs
	$affID   - affiliate id. 0 - not affiliate
	return ItemPrice():
		"date" 		  - estimated date
		"adult_count" - adult count
		"adult_price" - adult price
		"child_count" - child total
		"child_price" - child price
		"com_adult_price" - commisionable adult price
		"com_child_price" - commisionable child price
		"discount_string" - discount text
		"discount_total"  - discount total
		"total"  		  - total amount
  */
  function calculatePrice($userID,$date,$adults,$childs,$affID,$isGC)
  {
       $log = new LogsBO();

       $affiliate_discount_adult_amount = 0;
       $affiliate_discount_child_amount = 0;
       $affiliate_discount_string       = "";
       $overrride_specials              = false;
      
       $adult_price = $this->adult_price;
       $child_price = $this->child_price;

       $adult_price_orig = $this->adult_price;
       $child_price_orig = $this->child_price;

       $adult_price_new = $this->adult_price;
       $child_price_new = $this->child_price;
       
	   $com_adult_price = $this->adult_com_price;
   	   $com_child_price = $this->child_com_price;

   	   $discount_string = "";
   	   $com_total = 0;
   	   $total = 0;
   	   
   	   $isBOGOF = false;
   	   $isBOGOD = false;
   	   
       $free_adults_tickets = 0;
       $free_childs_tickets = 0;
   	   
       $is_affiliate     = false;
       if(strlen($affID)>0) //is affiliate
       {
            $affiliate = new AffiliateBO();
            $affiliate->id = $affID;
            $affiliate->confirmed = "1";
            $affiliate->blocked   = "0";
            $affiliate->Load(0,1);
            if($affiliate->Next())
            {
                $is_affiliate = true;
                if ($affiliate->override_specials == 1)  //overrride specials
                {
                     $overrride_specials = true;
                }			                                    
            }      
       }

   	   //if additional price exists
       if($this->addprice_exist==1)
       {
       	   $current_day_of_week = date("w", strtotime($date));       	//0 - sunday, 1- monday ...

       	   $field_value = 0;
       	   switch ($current_day_of_week)
       	   {
    		    case 0:
    				$field_value = $this->price_sunday;
    		        break;
    		    case 1:
    		        $field_value = $this->price_monday;
    		        break;
    		    case 2:
    		        $field_value = $this->price_tuesday;
    		        break;
    		    case 3:
    		        $field_value = $this->price_wednesday;
    		        break;
    		    case 4:
    		        $field_value = $this->price_thursday;
    		        break;
    		    case 5:
    		        $field_value = $this->price_friday;
    		        break;
    		    case 6:
    		        $field_value = $this->price_saturday;
    		        break;
		    }

		    if($field_value==1)
		    {
		       $adult_price = $this->addprice_adult;
		       $child_price = $this->addprice_child;

		       $adult_price_orig = $this->addprice_adult;
		       $child_price_orig = $this->addprice_child;

			   $com_adult_price = $this->addpricecom_adult;
	       	   $com_child_price = $this->addpricecom_child;
		    }else
		    {
		       if($is_affiliate)
		       {
                   $prices = DailyPricesBO::returnPrice($date,$this,$affiliate->id);
		       }else
		       {
		           $prices = DailyPricesBO::returnPrice($date,$this);    
		       }
		       
		       $adult_price = $prices[0];
		       $child_price = $prices[1];

		       $adult_price_orig = $adult_price;
		       $child_price_orig = $child_price;

			   $com_adult_price = $adult_price;
	       	   $com_child_price = $child_price;
		    }
       }
      
       //total price without discount
       $without_discount = $adults*$adult_price_orig + $childs*$child_price_orig;
       if($is_affiliate) //is affiliate
       {
            if($affiliate->discountAmount>0 && $this->provider==1)
            {
                if ($affiliate->discountType == 0)
                {
                     $affiliate_discount_adult_amount = ($adult_price/100) * $affiliate->discountAmount;
                     $affiliate_discount_child_amount = ($child_price/100) * $affiliate->discountAmount;
                     $affiliate_discount_string       = $affiliate->discountAmount."%";
                }else  if($affiliate->discountType == 1)
                {
                     $affiliate_discount_adult_amount = $affiliate->discountAmount;
                     $affiliate_discount_child_amount = $affiliate->discountAmount;
                     $affiliate_discount_string = "$".$affiliate->discountAmount;
                }
            }
       }
      
       if($overrride_specials) //override all specials and apply only affiliate's discount
       {
           if($affiliate_discount_adult_amount>0)
           {
               $adult_price = round($adult_price - $affiliate_discount_adult_amount,2);
               $child_price = round($child_price - $affiliate_discount_child_amount,2);

               $com_adult_price = $com_adult_price>0 ? round($com_adult_price - $affiliate_discount_adult_amount,2) : 0;
               $com_child_price = $com_child_price>0 ? round($com_child_price - $affiliate_discount_child_amount,2) : 0;
               
               $adult_price_new = $adult_price;
               $child_price_new = $child_price; 
           }
           
           $total = round($adults*$adult_price + $childs*$child_price,2);
       }else //calculate all specials + affiliate discount  
       {
           $item_discount   = new SiteDiscountBO();
           $discount_string = $item_discount->returnDiscount($this->id,$date,$is_affiliate,$userID,$affID);    

           $isBOGOF = $item_discount->isBOGOF($this->id, $date, $is_affiliate, $userID);
           if($isBOGOF)
           {
              //here need to calculate how much free tickets
              $free_adults_tickets = floor($adults / 2);
              $free_childs_tickets = floor($childs / 2);
           }
           
           $bogod_res    = $item_discount->isBOGOD($this->id, $date, $is_affiliate, $userID);
           $isBOGOD      = $bogod_res["exist"];
           $bogod_type   = $bogod_res["type"];
           $bogod_amount = $bogod_res["amount"];
           $bogod_adults_tickets = 0;
           $bogod_childs_tickets = 0;           
           if($isBOGOD)
           {
               
              //here need to calculate how much free tickets
              $bogod_adults_tickets = floor($adults / 2);
              $bogod_childs_tickets = floor($childs / 2);

                		if($this->title=="TEST")
                		{                		    
                		    $log->write(0,"admin","adults:".$adults);
                		    $log->write(0,"admin","childs:".$childs);
                		    $log->write(0,"admin","floor adults:".floor($adults / 2));
                		    $log->write(0,"admin","floor childs:".floor($childs / 2));                		                    		                    		    
                		}                  
           }
                      
           $total_bogof_discount = 0;
           $total_bogod_discount = 0;
           
           if(strlen($discount_string)>0)
           {           
               if($is_affiliate) //is affiliate
               {               
                   $adult_price_discounted = $item_discount->returnPrice($this->id, $adult_price, $date, $is_affiliate, $userID, $affiliate->id);
                   $child_price_discounted = $item_discount->returnPrice($this->id, $child_price, $date, $is_affiliate, $userID, $affiliate->id);
               }else
               {
                   $adult_price_discounted = $item_discount->returnPrice($this->id, $adult_price, $date,$is_affiliate,$userID);
                   $child_price_discounted = $item_discount->returnPrice($this->id, $child_price, $date,$is_affiliate,$userID);                   
               }
               
               $discount_adult_amount = $adult_price_orig - $adult_price_discounted;
               $discount_child_amount = $child_price_orig - $child_price_discounted;

               //here we checking if affiliate discount amount bigger than item discount
               //need to apply ONLY bigger discount              
               if($affiliate_discount_adult_amount > $discount_adult_amount)
               {
                     $discount_string = $affiliate_discount_string;
        
                     $adult_price = round ($adult_price - $affiliate_discount_adult_amount,2);
                     $child_price = round ($child_price - $affiliate_discount_child_amount,2);
                     $adult_price_discounted = $adult_price;
                     $child_price_discounted = $child_price;
        
                     $discount_adult_amount = $affiliate_discount_adult_amount;
                     $discount_child_amount = $affiliate_discount_child_amount;
               }else
               {
                     //erase affiliate discount text because we will not apply affiliate discount
                     $affiliate_discount_string = "";
               }

               $adult_price_new = $adult_price_discounted;
               $child_price_new = $child_price_discounted;
        
               $com_adult_price = $com_adult_price>0 ? round($com_adult_price - $discount_adult_amount,2) : 0;
               $com_child_price = $com_child_price>0 ? round($com_child_price - $discount_child_amount,2) : 0;

               $total = $adults*$adult_price_discounted + $childs*$child_price_discounted;
                                             
               $total = round($total,2);
           }else
           {              
                   if($affiliate_discount_adult_amount>0)
                   {
                       $adult_price = round($adult_price - $affiliate_discount_adult_amount,2);
                       $child_price = round($child_price - $affiliate_discount_child_amount,2);
        
                       $com_adult_price = $com_adult_price>0 ? round($com_adult_price - $affiliate_discount_adult_amount,2) : 0;
                       $com_child_price = $com_child_price>0 ? round($com_child_price - $affiliate_discount_child_amount,2) : 0;
                       
                       $adult_price_new = $adult_price;
                       $child_price_new = $child_price; 
                       $discount_string = $affiliate_discount_string;
                   }

                   $total = round($adults*$adult_price_new + $childs*$child_price_new,2);
                   
                   if($isBOGOF)
                   {               
                       $total_bogof_discount = round(
                            $free_adults_tickets*$adult_price_new +
                            $free_childs_tickets*$child_price_new,2);
                       $total = $total - $total_bogof_discount;
    
                       if($total_bogof_discount>0) $discount_string  = " BOGOF*";
                   }else if($isBOGOD)
                   {               
                   		if ($bogod_type == "%") 
                		{
                            $total_bogod_discount = round(
                            $bogod_adults_tickets*(($adult_price_new/100) * $bogod_amount) +
                            $bogod_childs_tickets*(($child_price_new/100) * $bogod_amount),2);            			
                		}else
                		{
                            $total_bogod_discount = round($bogod_adults_tickets*$bogod_amount + $bogod_childs_tickets*$bogod_amount,2);            			            			
                		}
                		
                		$total = $total - $total_bogod_discount;
                		
                		if($this->title=="TEST")
                		{                		    
                		    $log->write(0,"admin","bogod_amount:".$bogod_amount);
                		    $log->write(0,"admin","adult_price_new:".$adult_price_new);
                		    $log->write(0,"admin","bogod_adults_tickets:".$bogod_adults_tickets);
                		    $log->write(0,"admin","child_price_new:".$child_price_new);
                		    $log->write(0,"admin","bogod_childs_tickets:".$bogod_childs_tickets);                		                    		    
                		    $log->write(0,"admin","Total BOGOD Discount:".$total_bogod_discount." - ".$total);
                		}    
                		                		
                		if($total_bogod_discount>0) $discount_string  = " BOGOD*";
                   }                                    

                   $total = round($total,2);
                   //$discount_string = $affiliate_discount_string;
           }           
       }//end of specials processing

       if($com_adult_price==0)
       {
         $com_adult_price = $adult_price_new;            
       }
       
       if($com_child_price==0)
       {
          $com_child_price = $child_price_new;
       }
       
       $com_total = $adults*$com_adult_price + $childs*$com_child_price;

       if($isBOGOF)
       {
            $com_total = $com_total - $free_adults_tickets*$com_adult_price - $free_childs_tickets*$com_child_price;                 
       }else if($isBOGOD)
       {
       		if ($bogod_type == "%") 
    		{
                $com_total = $com_total - 
                $bogod_adults_tickets*(($com_adult_price/100) * $bogod_amount) - 
                $bogod_childs_tickets*(($com_child_price/100) * $bogod_amount);            			
    		}else
    		{
                $com_total = $com_total - $bogod_adults_tickets*$bogod_amount - $bogod_childs_tickets*$bogod_amount;            			
    		}          
       }
       
       $com_total = round($com_total,2);
       
       $item_price = new ItemPrice();   
       $item_price->date            = $date;
       $item_price->adult_count     = $adults;
       $item_price->child_count     = $childs;   
       $item_price->adult_price_orig = $adult_price_orig;
       $item_price->child_price_orig = $child_price_orig;   
       $item_price->adult_price     = $adult_price_new;
       $item_price->child_price     = $child_price_new;
       $item_price->com_adult_price = $com_adult_price;
       $item_price->com_child_price = $com_child_price;
       $item_price->discount_total  = round($without_discount-$total,2);
       $item_price->discount_string = $discount_string;
       $item_price->com_total       = $com_total;       
       $item_price->total           = $total;
                                  
       return $item_price;
  }  
}
class ItemPrice{
       var $date = "";
       var $adult_count     = 0;
       var $child_count     = 0;       
       var $adult_price_orig = 0;
       var $child_price_orig = 0;
       var $adult_price     = 0;
       var $child_price     = 0;       
       var $com_adult_price = 0;
       var $com_child_price = 0;
       var $discount_total  = 0;
       var $discount_string = "";
       var $com_total       = 0;       
       var $total           = 0;
           
}
?>